<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="老人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入老人姓名"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item label="用药时间" prop="useTime">
        <el-date-picker
          v-model="queryParams.useTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['health:medicineRegister:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['health:medicineRegister:plan']" @click="handleClickPlan">
          计划用药登记
        </el-button>
        <el-button v-hasPermi="['health:medicineRegister:temporary']" @click="handleClickTemporary">
          临时用药登记
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="HealthMedicineRegister"
      height-per="90"
      @page-change="getPage"
    >
      <template #medicineType="{ row }">
        {{ getDictObj(DICT_TYPE.HEALTH_MEDICINE_TYPE, row.medicine.medicineType).label }}
      </template>
      <template #medicineDosageForm="{ row }">
        {{
          getDictObj(DICT_TYPE.HEALTH_MEDICINE_DOSAGE_FORM, row.medicine.medicineDosageForm).label
        }}
      </template>
      <template #useType="{ row }">
        {{ getDictObj(DICT_TYPE.HEALTH_MEDICINE_REGISTER_TYPE, row.useType).label }}
      </template>
    </Table>
  </ContentWrap>
  <HealthMedicineRegisterPlan ref="healthMedicineRegisterPlanRef" @success="getPage" />
  <HealthMedicineRegisterTemporary ref="healthMedicineRegisterTemporaryRef" @success="getPage" />
</template>
<script setup name="HealthMedicineRegister" lang="ts">
import * as HealthMedicineRegisterApi from '@/api/health/medicineRegister'
import HealthMedicineRegisterPlan from './HealthMedicineRegisterPlan.vue'
import HealthMedicineRegisterTemporary from './HealthMedicineRegisterTemporary.vue'
import { DICT_TYPE, getDictObj } from '@/utils/dict'
const healthMedicineRegisterPlanRef = ref()
const healthMedicineRegisterTemporaryRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  useTime: undefined
})
const queryData = ref()
const columns = [
  {
    label: '老人姓名',
    prop: 'elder.name'
  },
  {
    label: '药品名称',
    prop: 'medicine.medicineName'
  },
  {
    label: '药品简码',
    prop: 'medicine.medicineBrevityCode'
  },
  {
    label: '药品类型',
    prop: 'medicine.medicineType',
    slot: 'medicineType'
  },
  {
    label: '药品规格',
    prop: 'medicine.medicineSpecification'
  },
  {
    label: '药品剂型',
    prop: 'medicine.medicineDosageForm',
    slot: 'medicineDosageForm'
  },
  {
    label: '用药数量',
    prop: 'useNumber'
  },
  {
    label: '用药时间',
    prop: 'useTime'
  },
  {
    label: '用药类型',
    prop: 'useType',
    slot: 'useType'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 初始化 */
onMounted(() => {
  handleClickSearch()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 计划用药登记按钮操作 */
const handleClickPlan = () => {
  healthMedicineRegisterPlanRef.value.open()
}

/** 临时用药登记按钮操作 */
const handleClickTemporary = () => {
  healthMedicineRegisterTemporaryRef.value.open()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await HealthMedicineRegisterApi.pageQuery(queryParams.value)
}
</script>
